package com.yangming.springboot.deepin.playTest;

import com.yangming.springboot.deepin.Application;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @创建人 ym
 * @创建时间 2021/4/1 0001
 * @描述
 **/
@SpringBootTest(classes = {Application.class})
public class DeaLockDemo {

    private static String a = "a";
    private static String b = "b";

    public static void main(String[] args){
        new DeaLockDemo().deadLock();
    }

    private void deadLock(){

        //说实话，没看懂，先放着了叭
        Thread t1 = new Thread(new Runnable(){
            @Override
            public void run(){
                synchronized(a){
                    try{
                        //如果把这个睡眠给关闭的话，也不会造成死锁
                       Thread.currentThread().sleep(2000);
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }
                    synchronized(b){
                        System.out.println("1");
                    }
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized(b){
                    synchronized(a){
                        System.out.println("2");
                    }
                }
            }
        });
        //两个一起打开就会死锁
        t1.start();
        t2.start();
    }

}
